package com.shanjupay.merchant.controller;

import com.shanjupay.merchant.api.MerchantService;
import com.shanjupay.merchant.api.dto.MerchantDTO;
import com.shanjupay.merchant.convert.MerchantDetailConvert;
import com.shanjupay.merchant.convert.MerchantRegisterConvert;
import com.shanjupay.merchant.service.FileService;
import com.shanjupay.merchant.service.SmsService;
import com.shanjupay.merchant.utils.SecurityUtil;
import com.shanjupay.merchant.vo.MerchantDetailVO;
import com.shanjupay.merchant.vo.MerchantRegisterVO;
import io.swagger.annotations.*;
import org.apache.dubbo.config.annotation.Reference;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.UUID;

/**
 * @program: shanjupay
 * @description:
 * @author: Mr.Hkc
 * @create: 2020-08-18 11:38
 **/

@RestController
@Api(value = "商户平台应用接口", tags = "商户平台应用接口", description = "商户平台应用接口")
public class MerchantController {

    @Reference
    MerchantService merchantService;

    @Autowired
    private SmsService smsService;

    @Autowired
    private FileService fileService;


    @ApiOperation(value = "资质申请")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "merchantInfo", value = "商户认证资料", required = true, dataType = "MerchantDetailVO", paramType = "body")
    })
    @PostMapping("/my/merchants/save")
    public void saveMerchant(@RequestBody MerchantDetailVO merchantInfo){
        //解析token,取出当前登录商户id
        Long merchantId = SecurityUtil.getMerchantId();
        System.out.println(merchantId);
        MerchantDTO merchantDTO = MerchantDetailConvert.INSTANCE.vo2dto(merchantInfo);
        merchantService.applyMerchant(merchantId,merchantDTO);
    }

    @ApiOperation(value = "根据id查询商户信息")
    @GetMapping("/merchants/{id}")
    public MerchantDTO queryMerchantById(@PathVariable("id") Long id) {
        return merchantService.queryMerchantById(id);
    }

    @ApiOperation("获取登录用户的商户信息")
    @GetMapping(value="/my/merchants")
    public MerchantDTO getMyMerchantInfo(){
        Long merchantId = SecurityUtil.getMerchantId();
        return merchantService.queryMerchantById(merchantId);
    }


    @ApiOperation(value = "获取手机验证码")
    @ApiImplicitParam(name = "phone", value = "手机号", required = true, dataType = "String", paramType = "query")
    @GetMapping("/sms")
    public String getSMSCode(@RequestParam String phone) {
        //向验证码服务请求发送验证码
        return smsService.sendMsg(phone);
    }


    @ApiOperation(value = "注册商户")
    @ApiImplicitParam(name = "merchantRegister", value = "注册信息", required = true, dataType = "MerchantRegisterVO", paramType = "body")
    @PostMapping("/merchants/register")
    public MerchantRegisterVO registerMerchant(@RequestBody MerchantRegisterVO merchantRegister) {

        //校验验证码
        smsService.checkVerifyCode(merchantRegister.getVerifiykey(), merchantRegister.getVerifiyCode());
        //注册商户 调用dubbo服务
        //MerchantDTO merchantDTO = new MerchantDTO();
        //向DTO写入商户注册信息
      /*  merchantDTO.setMobile(merchantRegister.getMobile());
        merchantDTO.setUsername(merchantRegister.getUsername());*/
        MerchantDTO merchantDTO = MerchantRegisterConvert.INSTANCE.vo2dto(merchantRegister);
        //..
        merchantService.createMerchant(merchantDTO);
        return merchantRegister;
    }

    //上传证件照片
    @ApiOperation(value = "上传证件照片")
    @PostMapping("/upload")
    public String upload(@ApiParam(value = "上传证件照", required = true) @RequestParam("file") MultipartFile file) throws IOException {
        //生成文件名称 需要保证唯一
        //文件原始名称
        String originalFilename = file.getOriginalFilename();
        //扩展名
        assert originalFilename != null;
        String suffix = originalFilename.substring(originalFilename.lastIndexOf(".") - 1);
        //文件名称
        String fileName = UUID.randomUUID() + suffix;
        return fileService.upload(file.getBytes(), fileName);
    }

}

